<?
session_start();
include_once 'includes/utilities.php';
$link = OpenDB();
if (isset($_SESSION["protocolo"]))
{
  $protocolo = $_SESSION["protocolo"];

  if (is_numeric($protocolo))
  {
    $pap = GetSQLFirstValue("select id_pap from pap where numero_protocolo = " . $protocolo);
    if (!is_numeric($pap))
    {
      $transaction = PostSQL("LOCK TABLES pap WRITE,  pap_desc_microscopica WRITE");
      $pap = GetSQLFirstValue("select IFNULL(max(id_pap),0) + 1 from pap");
      $insert = PostSQL("insert into pap (id_pap, numero_protocolo) values (".$pap.", ".$protocolo.")");
    }
    else
    {
        $clear = PostSQL("delete from pap_desc_microscopica where id_pap = ".$pap);
    }
      foreach ($_POST as $key => $value)
      {
        if ($value == "on")
        {
          $insert = PostSQL("insert into pap_desc_microscopica (id_pap, id_desc_microscopica) values
                            (".$pap.", '".$key."')");
        }
        if(substr_count($key, "radio_"))
        {
            $insert = PostSQL("insert into pap_desc_microscopica (id_pap, id_desc_microscopica) values
                            (".$pap.", '".$value."')");
        }
        
      }
      $transaction = PostSQL("COMMIT");
      $transaction = PostSQL("UNLOCK TABLES");
      CloseDB($link);
      header("Location: protPapDiagnostico.php");
    }
  } else
  {
    CloseDB($link);
    header("Location: error.php?error=No se encontro numero de protocolo");
  }
?>
